/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver;

import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Helper class for logging in this application.
 * <p>
 * AndroidAssembly1 this class was added in Android Assembly 1
 * <p>
 * <b>Thread safety:</b> This class is immutable and so thread safe.
 *
 * @author 7realm
 * @version 1.0
 */
public final class LogUtil {
	public static boolean enableLog = false;
	
    /**
     * This private constructor prevents to create a new instance.
     */
    private LogUtil() {
        // empty
    }

    public static Logger getLogger(String loggerName) {
    	if (!enableLog) {
    		return null;
    	}
    	return Logger.getLogger(loggerName);
    }
    /**
     * Logs method enter.
     *
     * @param logger the logger to use
     * @param clazz the object, where logging is done
     * @param methodName the name of method, where logging is done
     * @param params the method parameters
     */
    public static void enter(Logger logger, Object clazz, String methodName, Object... params) {
        if (logger != null) {
            logger.entering(clazz.getClass().getSimpleName(), methodName, params);
        }
    }

    /**
     * Logs method exit.
     *
     * @param logger the logger to use
     * @param clazz the object, where logging is done
     * @param methodName the name of method, where logging is done
     */
    public static void exit(Logger logger, Object clazz, String methodName) {
        if (logger != null) {
            logger.exiting(clazz.getClass().getSimpleName(), methodName);
        }
    }

    /**
     * Logs method exit.
     *
     * @param <T> the type of result
     * @param logger the logger to use
     * @param clazz the object, where logging is done
     * @param methodName the name of method, where logging is done
     * @param result method result
     * @return the method result
     */
    public static <T> T exit(Logger logger, Object clazz, String methodName, T result) {
        if (logger != null) {
            logger.exiting(clazz.getClass().getSimpleName(), methodName, result);
        }
        return result;
    }

    /**
     * Logs exception.
     *
     * @param <T> type of occurred exception
     * @param logger the logger to use
     * @param clazz the object, where logging is done
     * @param methodName the name of method, where logging is done
     * @param e the exception, that occurred in method
     * @return thrown exception
     */
    public static <T extends Throwable> T error(Logger logger, Object clazz, String methodName, T e) {
        if (logger != null) {
            logger.logp(Level.SEVERE, clazz.getClass().getSimpleName(), methodName, e.getMessage(), e);
            
            DebugHelper.recordException(e.getMessage());
        }
        return e;
    }

    /**
     * Logs info message.
     *
     * @param logger the logger to use
     * @param clazz the object, where logging is done
     * @param methodName the name of method, where logging is done
     * @param format the message format
     * @param params the format parameters
     */
    public static void info(Logger logger, Object clazz, String methodName, String format, Object... params) {
        if (logger != null) {
            preProcessParams(params);
            logger.logp(Level.INFO, clazz.getClass().getSimpleName(), methodName, MessageFormat.format(format, params));
        }
    }

    /**
     * Logs warning message.
     *
     * @param logger the logger to use
     * @param clazz the object, where logging is done
     * @param methodName the name of method, where logging is done
     * @param format the message format
     * @param params the format parameters
     */
    public static void warn(Logger logger, Object clazz, String methodName, String format, Object... params) {
        if (logger != null) {
            preProcessParams(params);
            logger.logp(Level.WARNING, clazz.getClass().getSimpleName(), methodName, MessageFormat.format(format,
                params));
        }
    }

    /**
     * Preprocess numeric properties to make their representation local independent.
     *
     * @param params the message params
     */
    @SuppressWarnings("boxing")
    private static void preProcessParams(Object... params) {
        for (int i = 0; i < params.length; i++) {
            if (params[i] instanceof Integer) {
                params[i] = Integer.toString((Integer) params[i]);
            } else if (params[i] instanceof Long) {
                params[i] = Long.toString((Long) params[i]);
            } else if (params[i] instanceof Double) {
                params[i] = Double.toString((Double) params[i]);
            }
        }
    }

}
